package com.aotain.oframework.util.bean;

import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.IOUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aotain.oframework.util.DateTypeAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.json.JettisonMappedXmlDriver;

/**
 * @author Jacob.cw.Zhang@gmail.com
 */
public class JSONUtil {
	
	private static Logger logger = LoggerFactory.getLogger(JSONUtil.class);
	
	public static String toJson(String request) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
		Gson gson = gsonBuilder.create();
		return gson.toJson(request);
	}

	public static Object fromJson(String json, Type type) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
		Gson gson = gsonBuilder.create();
		return gson.fromJson(json, type);
	}
	
	public static <T> T fromJson(String json, Class<T> clazz) {
		GsonBuilder gsonBuilder = new GsonBuilder();
		gsonBuilder.registerTypeAdapter(Date.class, new DateTypeAdapter());
		Gson gson = gsonBuilder.create();
		return gson.fromJson(json, clazz);
	}
	
	public static String toJson(HttpServletRequest request) {
		JSONArray jsonArray = null;
		StringBuffer buffer = new StringBuffer();
		try {
			InputStreamReader isr = new InputStreamReader(request.getInputStream());
			String str = IOUtils.toString(isr);
			logger.debug("JSONUtil str {}" , str);
			jsonArray = new JSONArray(str);
			for (int i = 0; i < jsonArray.length(); i++) {
				buffer.append(jsonArray.getJSONArray(i));
			}
			logger.debug("JSON string {}" , buffer);
		} catch(IOException e){
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return buffer.toString();
	}
	
	public static List<Object> jsonToObject(HttpServletRequest request, Object obj) {
		List<Object> objs = new ArrayList<Object>();
		try {
			Class classType = obj.getClass();
			System.out.println(" classType : " + classType);
			JSONArray jsonArray = null;
			StringBuffer jsonStr = new StringBuffer();
			Object newObj = classType.getConstructor(new Class[] {}).newInstance(new Object[] {});
			InputStreamReader is = new InputStreamReader(request.getInputStream());
			String str = IOUtils.toString(is);
			System.out.println(" JSONUtil str : " + str);
			jsonArray = new JSONArray(str);
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject jsonObject = jsonArray.getJSONObject(i);
				XStream xstream = new XStream(new JettisonMappedXmlDriver());
				xstream.alias(newObj.getClass().getSimpleName(), newObj.getClass());
				newObj = xstream.fromXML(jsonObject.toString());
				objs.add(newObj);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return objs;
	}
	
}
